﻿/// <reference path="../../../../SIR.Web/scripts/_references.js" />
/// <reference path="../../../../SIR.Web/scripts/jquery-1.9.1.js" />
/// <reference path="../../../../SIR.Web/scripts/angular.js" />
/// <reference path="../../../../SIR.Web/scripts/angular-mocks.js" />

/// <reference path="../../../../SIR.Web/app/app.js" />
/// <reference path="../../../../SIR.Web/app/common/common.js" />
/// <reference path="../../../../SIR.Web.Tests/JavaScript/sir-mock.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/authenticate.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/services/auth-service.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/controllers/user-control-panel-controller.js" />

describe('User Control Panel Controller - Reset Password:', function () {
	'use strict';
	// declare variables and modules
	var scope, location, controller, rootScope, $httpBackend, notifyService;

	beforeEach(module('ui.router'));
	beforeEach(module('app.common'));
	beforeEach(module('app.authenticate'));

	beforeEach(inject(function ($injector, $controller, $rootScope, authService, notify) {
		// set up variables for each test
		$httpBackend = $injector.get('$httpBackend');
		rootScope = $rootScope;
		scope = $rootScope.$new();
		// expect the log in request
		$httpBackend.expectPOST('/api/Authentication/LogIn').respond({ Successful: false, Content: null, Message: '' });
		// create the controller
		controller = $controller('ControlPanelController', { $scope: scope, $state: {}, authService: authService });
		$httpBackend.flush();
		// spy on the notification service
		notifyService = notify;
		spyOn(controller, 'handlePasswordResetReply').andCallThrough();
		spyOn(controller, 'showLogInState').andCallThrough();
		spyOn(notifyService, 'warning').andCallThrough();
		spyOn(notifyService, 'success').andCallThrough();

	}));
	// expect all requests have been handled
	afterEach(function () {
		$httpBackend.verifyNoOutstandingExpectation();
		$httpBackend.verifyNoOutstandingRequest();
	});

	describe('Controller - User Reset Password (Total: 3)', function () {
		it('[1] Produce \'Email is required\' message when user attempts to reset their password with an empty email', function () {
			// set the email to empty
			scope.model.email = '';
			// try to reset password
			scope.resetPassword();
			// expect that still in the password reset state
			expect(controller.showLogInState).wasNotCalled();
			// expect the warning message was shown
			expect(notifyService.warning).toHaveBeenCalledWith('Email is required');
			// expect values to be clear
			expect(scope.user).toBe(null);
			expect(scope.model.email).toBe('');
		});


		it('[2] Produce \'Password reset successfully\' message when user enters an invalid email to reset password', function () {
			// expect the password reset request and return a successful response
			$httpBackend.expectPOST('/api/Authentication/ResetPassword').respond({ Successful: true, Content: null, Message: 'Password reset successfully' });
			// set the email value
			scope.model.email = 'email';
			// reset the password
			scope.resetPassword();
			scope.$digest();
			$httpBackend.flush();
			// expect the handler was called
			expect(controller.handlePasswordResetReply).toHaveBeenCalled();
			// expect that the log in state is active
			expect(controller.showLogInState).toHaveBeenCalled();
			// expect the success message was shown to the user
			expect(notifyService.success).toHaveBeenCalledWith('Password reset successfully');
			// expect the user is still null
			expect(scope.user).toBe(null);
			// expect that the email was not cleared
			expect(scope.model.email).toBe('email');
		});

		it('[3] Produce \'Reset password sent to email\' message when user enters a valid email to reset password', function () {
			// expect the password reset request and return a successful response
			$httpBackend.expectPOST('/api/Authentication/ResetPassword').respond({ Successful: true, Content: null, Message: 'Password reset successfully' });
			// set the email value
			scope.model.email = 'email';
			// reset the password
			scope.resetPassword();
			scope.$digest();
			$httpBackend.flush();
			// expect the handler was called
			expect(controller.handlePasswordResetReply).toHaveBeenCalled();
			// expect that the log in state is active
			expect(controller.showLogInState).toHaveBeenCalled();
			// expect the success message was shown to the user
			expect(notifyService.success).toHaveBeenCalledWith('Password reset successfully');
			// expect the user is still null
			expect(scope.user).toBe(null);
			// expect that the email was not cleared
			expect(scope.model.email).toBe('email');
		});

	});
});